package ${serviceImplPath};

import ${entityPath}.${EntityName};
import ${dtoPath}.*;
import ${voPath}.${EntityName}Vo;
import ${mapperPath}.${EntityName}Mapper;
import ${servicePath}.I${EntityName}Service;
import java.util.List;
import java.util.Date;
import lombok.extern.slf4j.Slf4j;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import com.lenovo.cnbu.common.core.enums.BooleanTypeEnum;
import com.lenovo.cnbu.common.core.utils.*;
import com.lenovo.cnbu.common.core.utils.bean.BeanUtils;
import com.lenovo.cnbu.common.security.utils.SecurityUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lenovo.cnbu.common.core.exception.ServiceException;
import java.util.ArrayList;
import java.util.Collections;

/**
* @Description:${table_annotation}Service实现类
* @author ${author}
* @date ${date}
*/
@Service
@Slf4j
@RequiredArgsConstructor(onConstructor = @_(@Autowired))
public class ${EntityName}ServiceImpl  extends ServiceImpl<${EntityName}Mapper,${EntityName}> implements I${EntityName}Service {

    /**
    *
    * ${table_annotation}Mapper
    */
    private final ${EntityName}Mapper ${lowName}Mapper;
<#if add>
    /**
    * 新增${table_annotation}
    * @param dto
    * @return {@link Long }
    */
    @Override
    public Long insert(${EntityName}AddDto dto){
        ${EntityName} ${lowName} = BeanUtils.copy(dto, ${EntityName}.class);
        checkForSave(${lowName});
    <#if isDeleted>
        ${lowName}.setIsDeleted(BooleanTypeEnum.NO.getCode());
    </#if>
    <#if createTime>
        ${lowName}.setCreateTime(new Date());
    </#if>
    <#if updateTime>
        ${lowName}.setUpdateTime(new Date());
    </#if>
    <#if createBy>
        ${lowName}.setCreateBy(SecurityUtils.getUsername());
    </#if>
    <#if updateBy>
        ${lowName}.setUpdateBy(SecurityUtils.getUsername());
    </#if>
        ${lowName}Mapper.insert(${lowName});
        log.info("新增成功{}",JacksonUtils.toString(${lowName}));
        return ${lowName}.getId();
    }
</#if>
<#if update>
    /**
    * 修改${table_annotation}
    * @param dto
    */
    @Override
    public void update(${EntityName}UpdateDto dto){
        ${EntityName} ${lowName} = BeanUtils.copy(dto, ${EntityName}.class);
        checkForSave(${lowName});
    <#if updateTime>
        ${lowName}.setUpdateTime(new Date());
    </#if>
    <#if updateBy>
        ${lowName}.setUpdateBy(SecurityUtils.getUsername());
    </#if>
        ${lowName}Mapper.updateById(${lowName});
        log.info("修改成功{}",JacksonUtils.toString(${lowName}));
    }
</#if>
    /**
    * 检查校验是否保存${table_annotation}
    *
    * @param ${lowName} ${table_annotation}
    */
    private void checkForSave(${EntityName} ${lowName}) {
        int count = this.count(Wrappers.lambdaQuery(${EntityName}.class)
<#if isDeleted>
            .eq(${EntityName}::getIsDeleted, BooleanTypeEnum.NO.getCode())
</#if>
            .eq(${EntityName}::getName, ${lowName}.getName())
            .ne(${lowName}.getId()!=null,${EntityName}::getId, ${lowName}.getId())
        );
        if (count > 0) {
            throw new ServiceException(MessageUtils.message("already.exist",MessageUtils.message("${lowName}")));
        }
    }
<#if delete>
    /**
    * 批量删除${table_annotation}
    * @param ids
    */
    @Override
    public void deleteBatch(List<Long> ids){
        List<${EntityName}> list = ${lowName}Mapper.selectBatchIds(ids);
    <#if isDeleted>
        list.forEach(${lowName} -> ${lowName}.setIsDeleted(BooleanTypeEnum.YES.getCode()));
        this.updateBatchById(list);
    <#else >
        this.removeByIds(ids);
    </#if>
        log.info("删除成功{}",JacksonUtils.toString(list));
    }
</#if>
<#if page>
    /**
    * ${table_annotation}分页查询
    *
    * @param dto
    * @return  {@link ${EntityName} }
    */
    @Override
    public List<${EntityName}> list(${EntityName}PageDto dto){
        LambdaQueryWrapper<${EntityName}> query = Wrappers.lambdaQuery(${EntityName}.class)
        <#if isDeleted>
            .eq(${EntityName}::getIsDeleted, BooleanTypeEnum.NO.getCode())
        </#if>
        ;
        return this.list(query);
    }
</#if>
<#if getVoById>
    /**
    * 根据id查询${table_annotation}
    * @param id
    * @return  {@link ${EntityName}Vo }
    */
    @Override
    public ${EntityName}Vo getVoById(Long id){
        ${EntityName} ${lowName} = this.getById(id);
        if (${lowName} == null) {
            return null;
        }
        return convertToVo(${lowName});
    }
</#if>
<#if getAll>
    /**
    *
    * 查询所有${table_annotation}
    * @return  {@link List<${EntityName}Vo> }
    */
    @Override
    public List<${EntityName}Vo> getAll(){
        LambdaQueryWrapper<${EntityName}> query = Wrappers.lambdaQuery(${EntityName}.class)
        <#if isDeleted>
              .eq(${EntityName}::getIsDeleted, BooleanTypeEnum.NO.getCode())
        </#if>
        ;
        List<${EntityName}> list = this.list(query);
        return convertToVo(list);
    }
</#if>
    /**
    *
    * 处理entity转换vo字段 List
    * @return  {@link List<${EntityName}Vo> }
    */
    @Override
    public List<${EntityName}Vo> convertToVo(List<${EntityName}> list) {
        if (list == null || list.isEmpty()){
            return new ArrayList<>();
        }
        List<${EntityName}Vo> voList = BeanUtils.copyList(list, ${EntityName}Vo.class);
        //处理entity转换vo字段
        for (${EntityName}Vo vo : voList) {

        }
        return voList;
    }
    /**
    *
    * 处理entity转换vo字段
    * @return  {@link ${EntityName} }
    */
    @Override
    public ${EntityName}Vo convertToVo(${EntityName} ${lowName}) {
        List<${EntityName}> list = Collections.singletonList(${lowName});
        List<${EntityName}Vo> voList = convertToVo(list);
        return voList.get(0);
    }
}
